package top.kangshuai.controller;

import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import top.kangshuai.model.UtilModel;
import top.kangshuai.service.UtilService;
import top.kangshuai.utils.ImageCode;

/**
 * 工具类型器
 * 
 * @author ks187
 *
 */
@Controller
@RequestMapping("/util")
public class UtilController {
  @Autowired
  private UtilService utilService;

  @RequestMapping("/validate.jpg")
  public void imageCode(UtilModel model, HttpServletResponse response) throws Exception {
   // /util/validate.jpg
    //自定义应答类型图片
    response.setContentType("image/jpeg");
    // 获取应答图片
    String code=utilService.makeImageCode(model);
    //将图片通过response输出
    OutputStream os=response.getOutputStream();
    ImageIO.write(ImageCode.makeImage(code), "jpg",os);
    os.close();
  }
}
